﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Fitment.Admin
{
    public partial class workerMng : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindList();
            }
            if (Session["LoginUser"] == null)    //上面Session名字用LoginUser,这里的名字要和上面的相同,可以随便改,保持一致就行了,注意:Session["LoginUser"]后面,没有ToString()哦,你要是加上的话,就会出错了.
            {
                Response.Redirect("logon.aspx"); //如果Session为空,则转到登陆页.
            }
        }

        DS data = new DS(); //新建数据库连接
        string strWorker = null;
        int  strPriority;   //分别表示工时、工种的优先级
        //选择编辑
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridView1.EditIndex = e.NewEditIndex;
            bindList();
        }

        //选择删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            strWorker = (this.GridView1.Rows[e.RowIndex].FindControl("type")as Label).Text.ToString();
            string strSql = "DELETE FROM WorkerInfo WHERE WorkType='"+strWorker+"'";
            if (data.ExcuteSql(strSql)) //执行删除操作
            {
                this.GridView1.EditIndex = -1;
                bindList();
            }  //重新绑定数据
            else return;

        }

        //选择分页
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;  //当翻页时指定页号
            bindList();   //重新绑定数据
        }

        //选择更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            strWorker = (this.GridView1.Rows[e.RowIndex].FindControl("type")as Label).Text.Trim();
          
            strPriority = Convert.ToInt32((this.GridView1.Rows[e.RowIndex].FindControl("priority")as TextBox).Text.Trim());
            string strSql = "UPDATE WorkerInfo SET WorkType='"+strWorker+"',Priority='"+strPriority+"' WHERE WorkType='"+strWorker+"'";
            if (data.ExcuteSql(strSql))
            {
                this.GridView1.EditIndex = -1;
                bindList(); 
            } //重新绑定控件
            else return;
        }

        protected void bindList()
        {

            string strSql = "SELECT *FROM WorkerInfo ORDER BY Priority ASC";
            DataTable table = data.GetDataSource(strSql, "Query");
            this.GridView1.DataSource = table.DefaultView;
            this.GridView1.DataBind();   //将数据绑定到控件中
            clear();
        }

        //用于复合条件查询
        protected void bindList(string sql)
        {
            //写入特定sql语句
            DataTable table = data.GetDataSource(sql, "Query");
            this.GridView1.DataSource = table.DefaultView;
            this.GridView1.DataBind();
            clear();
        }

       
        protected void addButton_Click(object sender, ImageClickEventArgs e)
        {
            strWorker = this.WorkType.Text.Trim();
           
            strPriority = Convert.ToInt32(this.Priority.Text.Trim());
            if (strWorker=="")
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('请填写完整信息！');</script>");
                return;
            }
            string strSql = "INSERT INTO WorkerInfo(WorkType,Priority)Values('" + strWorker + "','" + strPriority + "') ";
            if (data.ExcuteSql(strSql))
            {
                bindList(); // 当插入成功时，重新绑定控件
                
            }
            else
            {

                //当插入失败时返回
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('插入数据失败，请检查！');</script>");
                return;
            }
        }

        protected void CancelButton_Click(object sender, ImageClickEventArgs e)
        {
            clear();
        }

        protected void clear()
        {
            this.WorkType.Text = "";

        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            bindList();
            
        }

        protected void queryButton_Click(object sender, EventArgs e)
        {
            this.GridView1.EditIndex = -1;
            bindList();
           
        }

        protected void typeButton_Click(object sender, EventArgs e)
        {
            strWorker = this.search.Text.Trim();
            if (strWorker=="")
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('请输入工种信息！');</script>");
                return;
            }
            string strSql = "SELECT * FROM WorkerInfo WHERE WorkType='"+strWorker+"'";
            DataTable table = data.GetDataSource(strSql,"Query");
            if (table.Rows.Count==0)
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('工种不存在！');</script>");
                return;
            }
            bindList(strSql);
           

        }

       
    }
}
